package com.company.cloud.article.modules.mgr.controller;

import com.company.cloud.article.modules.mgr.controller.req.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.company.cloud.article.modules.cache.models.ArticleModel;
import com.company.cloud.article.modules.base.service.ArticleCategoryMappingInfoService;
import com.company.cloud.article.modules.base.service.ArticleInfoService;
import com.company.cloud.article.modules.cache.service.ArticleCacheService;
import com.company.cloud.article.modules.mgr.service.ArticleMgrService;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.utils.AuthContextUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 文章管理
 */
@Authc
@RestController
@RequestMapping("/mgr/cms/article")
@Api(tags = "Mgr_cms_文章管理")
public class ArticleMgrController {

    @Autowired
    private ArticleInfoService articleInfoService;
    @Autowired
    private ArticleCategoryMappingInfoService articleCategoryMappingInfoService;
    @Autowired
    private ArticleMgrService articleMgrService;
    @Autowired
    private ArticleCacheService articleCacheService;

    @PostMapping("/query")
    @ApiOperation(value = "查询", response = ArticleModel.class)
    public BaseResult query(@RequestBody ArticleMgrQuery4PageParam params) {
        return BaseResult.ok(articleMgrService.queryArticle(params, AuthContextUtils.getUser()));
    }

    @GetMapping("/get")
    @ApiOperation(value = "根据引用获取详细信息", response = ArticleModel.class)
    public BaseResult get(@RequestParam("companyArticleNo") String companyArticleNo) {
        ArticleModel data = articleCacheService.getCompanyArticleByNoCache(companyArticleNo);
        return BaseResult.ok(data);
    }


    @PostMapping("/addRel")
    @ApiOperation(value = "引用新增")
    public BaseResult addRel(@RequestBody ArticleRelAddParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        articleMgrService.addRel(params, userInfo);
        return BaseResult.ok();
    }

    @PostMapping("/addByRel")
    @ApiOperation(value = "引用批量新增")
    public BaseResult addBatchRel(@RequestBody ArticleRelAddListParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        articleMgrService.addRel(params, userInfo);
        return BaseResult.ok();
    }

    //==============================================
    @PostMapping("/editByRel")
    @ApiOperation(value = "修改引用资讯", response = BaseResult.class)
    public BaseResult editByRel(@RequestBody ArticleRelUpdateParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        articleMgrService.updateRel(params, userInfo);
        // 删除缓存
        articleCacheService.removeCompanyArticleByCache(params.getArticleRelNo());
        return BaseResult.ok();
    }

    @PostMapping("/updateStateByRel")
    @ApiOperation(value = "修改引用资讯状态(引用资讯上下架)", response = BaseResult.class)
    public BaseResult updateStateByRel(@RequestBody ArticleRelUpdateStateParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        articleMgrService.updateRelState(params, userInfo);
        return BaseResult.ok();
    }


    @PostMapping("/updateStateBatchByRel")
    @ApiOperation(value = "批量修改引用资讯状态(批量引用资讯上下架)", response = BaseResult.class)
    public BaseResult updateStateBatchByRel(@RequestBody ArticleRelUpdateStateBatchParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        articleMgrService.updateRelStateBatch(params,userInfo);
        return BaseResult.ok();
    }


    @GetMapping("/deleteByRel")
    @ApiOperation(value = "删除引用资讯")
    public BaseResult deleteByRel(@RequestParam("articleRelNo") String articleRelNo) {
        if (StringUtils.isEmpty(articleRelNo)) {

        }
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        articleMgrService.removeRel(articleRelNo, userInfo);
        // 删除缓存
        articleCacheService.removeCompanyArticleByCache(articleRelNo);
        return BaseResult.ok();
    }
}
